<?php
class UserItemController extends Zendvn_Controller_Action {
		
//Mang tham so nhan duoc khi mot Action chay
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
			'itemCountPerPage' => 8,
			'pageRange' => 4,
			'currentPage' => 1
	);
	protected $_namespace;
	
	//Url page
	protected $_page = '';
	
	public function init(){
		//Mang tham so nhan duoc khi mot Action chay
		$this->_arrParam = $this->_request->getParams();
	
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
	
		//Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index';	
	
		//Trang hien tai
		if(isset($this->_arrParam['page'])){
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}
		
		//Truyen thong tin phan trang vao mang du lieu
		$this->_arrParam['paginator'] = $this->_paginator;
		
	
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/public/" . $siteConfig['template']['site'];
		$this->loadTemplate($template_path, 'template.ini', 'user');
		
		$language = Zend_Registry::get('language');
		$this->view->language = $language['language'];
	}
	
	//Ham chay sau ham action
	public function postDispatch(){
		$siteConfig = Zend_Registry::get('siteConfig');
		$this->view->headMeta()->setName('description',$siteConfig['config_meta']['description']);
		$this->view->headMeta()->setName('keywords',$siteConfig['config_meta']['keywords']);
		$this->view->headMeta()->setHttpEquiv('Refresh',$siteConfig['config_meta']['refresh']);
		$this->view->headMeta()->setHttpEquiv('content-language',$siteConfig['config_meta']['content_language']);
		$this->view->headMeta()->setName('classification',$siteConfig['config_meta']['classification']);
		$this->view->headMeta()->setName('language',$siteConfig['config_meta']['language']);
		$this->view->headMeta()->setName('robots',$siteConfig['config_meta']['robots']);
		$this->view->headMeta()->setName('author',$siteConfig['config_meta']['author']);
		$this->view->headMeta()->setName('copyright',$siteConfig['config_meta']['copyright']);
		$this->view->headMeta()->setName('revisit-after',$siteConfig['config_meta']['revisit_after']);
		
		if($siteConfig['config_site']['offline'] == 1){
			$this->_forward('off','public','default');
		}
		
		$info 		= new Zendvn_System_Info();
		$user_info 	= $info->getMemberInfo();
		if(count($user_info) > 0){
			if($user_info['active_code'] != '0'){
				$this->_forward('enabled','public','default');
			}
		}
	}
		
	public function indexAction() {
		$this->view->Title = 'Quản lý tin đăng';
		$this->view->headTitle($this->view->Title, true);
	}
	
	public function addAction() {
		$this->view->Title = 'Đăng tin mới';
		$this->view->headTitle($this->view->Title, true);
		
		$tblItem = new Raovat_Model_UserItem();
		$this->view->slbCategory 	= $tblItem->itemInSelectbox($this->_arrParam, array('task' => 'user-category', 'action' => 'add'));
		$this->view->slbNhucau 		= $tblItem->itemInSelectbox(array('id_cat' => $this->_arrParam['id_cat']), array('task' => 'user-nhucau'));
		$this->view->slbChungloai	= $tblItem->itemInSelectbox(array('id_cat' => $this->_arrParam['id_cat']), array('task' => 'user-chungloai'));
		$this->view->slbCity		= $tblItem->itemInSelectbox($this->_arrParam, array('task' => 'user-city'));
		$this->view->slbDistrict	= $tblItem->itemInSelectbox(array('id_city' => $this->_arrParam['id_city']), array('task' => 'user-district'));
	}
	
	public function editAction(){
	
		$this->view->Title = 'Sửa thông tin';
		$this->view->headTitle($this->view->Title, true);
	
		$info 		= new Zendvn_System_Info();
		$user_info 	= $info->getMemberInfo();
	
		$tblUser	= new Default_Model_Users();
		$this->view->Item = $tblUser->getItem(array('id' => $user_info['id']),array('task' => 'public'));
	
		$tblCity = new Default_Model_Cities();
		$this->view->city = $tblCity->itemInSelectbox(null,array('task' => 'get-cities'));
	
		$tblRegistry = new Default_Model_Register();
	
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateRegistry($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>true));
				$tblRegistry->saveItem($arrParam,array('task'=>'user-edit'));
				$this->_redirect('default/user/index/save/ok');
			}
		}
	}
	
	public function nhucauAction(){
		$this->_helper->layout->disableLayout();
		$tblItem = new Raovat_Model_UserItem();
		$this->view->slbNhucau 		= $tblItem->itemInSelectbox(array('id_cat' => $this->_arrParam['id_cat']), array('task' => 'user-nhucau'));
	}
	
	public function chungloaiAction(){
		$this->_helper->layout->disableLayout();
		$tblItem = new Raovat_Model_UserItem();
		$this->view->slbChungloai	= $tblItem->itemInSelectbox(array('id_cat' => $this->_arrParam['id_cat']), array('task' => 'user-chungloai'));
	}
	
	public function districtAction(){
		$this->_helper->layout->disableLayout();
		$tblItem = new Raovat_Model_UserItem();
		$this->view->slbDistrict	= $tblItem->itemInSelectbox(array('id_city' => $this->_arrParam['id_city']), array('task' => 'user-district'));
	}
	
}
